package io.kiki.sba.registry.server.clientInterface.store;

import io.kiki.sba.registry.common.model.PublisherUtils;
import io.kiki.sba.registry.common.model.Tuple;
import io.kiki.sba.registry.common.model.store.Publisher;
import io.kiki.sba.registry.server.clientInterface.slot.SlotsCache;
import io.kiki.sba.registry.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;


public class PublishersImpl extends AbstractDataManager<Publisher> implements Publishers {

    @Autowired
    SlotsCache slotsCache;

    private final SlotStore<Publisher> slotStore = new SlotStore<>(this::slotOf);

    public PublishersImpl() {
        super();
    }

    @Override
    public boolean add(Publisher publisher) {
        StringUtil.nullValue(publisher.getVersion(), "publisher.version");
        StringUtil.nullValue(publisher.getRegisterTimestamp(), "publisher.registerTimestamp");

        PublisherUtils.internPublisher(publisher);
        Tuple<Publisher, Boolean> tuple = addData(publisher);
        return tuple.o2;
    }

    @Override
    public Map<String, Map<String, Publisher>> getDataInfoIdPublishers(int slotId) {
        return slotStore.copyMap(slotId);
    }

    @Override
    protected Store<Publisher> getSlotStore() {
        return slotStore;
    }


    protected int slotOf(String dataInfoId) {
        return slotsCache.slotOf(dataInfoId);
    }
}
